define(["app/service/MobileMsgService",'app/utils/UploadImgUtils'],function (MobileMsgService, UploadImgUtils) {

	var opt = {};

	function directive_login1_confirm(value){


		if($.isNullStr(value)){
			$.toast("验证码不能为空");
			return;
		}else if(value.length != 4){
			$.toast("请正确输入4位验证码");
			return;
		}

		var tel_value = $(".tel").val();
		var name_value = $(".name").val();
		var headImageUrl = $(".headImageUrl").attr("src");


		$.closeModal();

		$.processing("验证中...", 1500, function(){
			
			MobileMsgService.verifyLoginVerificationCode(tel_value,value, name_value, headImageUrl,opt.userUuid, function(resp){

				$.hidePreloader();
				if(resp.status!="200"){
					$.promptMobleMsgCode('请输入4为短信验证码',directive_login1_confirm);
					$.toast("验证码不正确或已过期");
				}else{
					
					$.closeModal();
					$.redirectTo($.wjtBaseFront + "/orgList/"+opt.userUuid + "?unionid="+opt.unionid);
				}						
			});
			
		});
		
	}


	function beforeuploadFunc(up,file){
		$.processing("头像上传中...", 1);
	}

	function uploadedFunc(info){
		$.hidePreloader();
		// Object {hash: "FtRJBAplyOckAHWWLts855RvdOy7", key: "o_1aonhfltv186g1n9gp7k1se01qf7f.jpg"}
		$("#avatar_img").attr("src",$.wjtBaseCdn + info.key+"?imageView2/5/w/118/h/118/q/100/format/jpg/interlace/1");
		var img = $("#avatar_img").clone();
		$("#avatar_img").remove();
		$("#avatar_container").append(img);
		
		UploadImgUtils.init("avatar_img","haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc);
	}

	function errorFunc(up, err, errTip){
		$.hidePreloader();
		if(err.code == -600){
			$.toast("上传的图片大小不能超过6MB");
		}else{
			$.toast("您的网络连接不太稳定，请重试");
		}
	}
	return {

		directive_inputName: function(){

			$(".name_cover").click(function(){

 				$.promptInput("请输入您的姓名",$(".name").val(), function(value){
 					//OK button
 					$(".name").val(value);

 				}, null, "text" , "");

			});
		},

		directive_inputTel: function(){

			$(".tel_cover").click(function(){

				$.promptInput("请输入11位手机号",$(".tel").val(), function(value){
					//OK button
					$(".tel").val(value);

				}, null,"tel" , "");
			});

		},
		
		directive_avatar1: function(){

			UploadImgUtils.init("avatar_img","haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc);

		},
		directive_weixinavatar: function(){

			$(".initweixinbutton").click(function(){

				$("#avatar_img").attr("src", $("#avatar_img").data("weixinavatarurl"));
				var img = $("#avatar_img").clone();
				$("#avatar_img").remove();
				$("#avatar_container").append(img);
				
				UploadImgUtils.init("avatar_img","haojsqiniu",beforeuploadFunc,uploadedFunc, errorFunc);
				
			});
		},
		directive_login1: function(){
			
			$(".directive_login1").click(function(e){
				var tel_value = $(".tel").val();
				var tel_orig_value = $(".tel").data("original");
				var name_value = $(".name").val();
				var name_orig_value = $(".name").data("original");
				var headImgUrl = $(".headImageUrl").attr("src");
				var headImgUrl_orig = $(".headImageUrl").data("original");

				var userUuid = $(this).data("uuid");
				opt.userUuid = userUuid;
				
				opt.unionid = $.getHashUrlParam("unionid");

				if($.isNullStr(name_value)){
					$.toast("姓名不能为空");
					return;
				}

				if(!$.isTel(tel_value)){
					$.toast("请正确输入11位手机号");
					return;
				}

				if($.isNullStr(headImgUrl)){
					headImgUrl = $.const.defaultAvatar;
				}

				if(tel_value == tel_orig_value && name_value == name_orig_value && headImgUrl==headImgUrl_orig){
	        		$.processing("登录中...", 1500, function(){
						$.hidePreloader();
						$.redirectTo($.wjtBaseFront + "/orgList/"+userUuid + "?unionid="+opt.unionid);
	        		});

				}else{
					
					$.processing("短信验证码发送中...", 1000, function(){

						MobileMsgService.sendVerificationCode(tel_value,function(resp){

							$.hidePreloader();
							if(resp.status!="200"){
								$.toast("验证码发送失败，请稍后再试");
							}else{
								$.promptMobleMsgCode('请输入4为短信验证码',directive_login1_confirm);
							}						
						});

					});	
						
					
				}
				

			});
		}
	};

});
